package com.example.guardian.anticheatguard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.example.guardian.R;
import com.example.guardian.home.setHeight;
import com.example.guardian.network.GetOkHttp;

import okhttp3.Call;
import okhttp3.Response;

public class anti_adapter extends BaseAdapter {
    private Context mContext;
    private List<newslist> mList = new ArrayList<>();

    public anti_adapter(Context context, List<newslist> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        newslist news_item = (newslist) mList.get(i);
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.viewpage_item, null);
            viewHolder.Title = (TextView) view.findViewById(R.id.anti_news_title);
            viewHolder.Content = (TextView) view.findViewById(R.id.anti_news_brief);
            viewHolder.anti_news_pic= (ImageView) view.findViewById(R.id.anti_pic);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.Title.setText(news_item.getnews_title());
        viewHolder.Content.setText(news_item.getnews_content());
        viewHolder.anti_news_pic.setImageResource(getDrawableId(mContext,news_item.getImname()));
        viewHolder.Title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemSpreadListener.onSpreadClick(i);
            }
        });
        return view;
    }

    public interface onItemSpreadListener {
        void onSpreadClick(int i);
    }

    private onItemSpreadListener mOnItemSpreadListener;

    public void setOnItemSpreadClickListener(onItemSpreadListener mOnItemSpreadListener) {
        this.mOnItemSpreadListener = mOnItemSpreadListener;
    }

    class ViewHolder {
        TextView Title;
        TextView Content;
        ImageView anti_news_pic;
    }

    public static int getDrawableId(Context context, String var) {

        try {
            int imageId = context.getResources().getIdentifier(var, "drawable", context.getPackageName());
            return imageId;
        } catch (Exception e) {
            return 0;
        }
    }





}
